package com.github.starry.fw.social.web.spider.wechat.tasklet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.github.starry.fw.social.web.spider.wechat.bean.ApiParameter;
import com.github.starry.fw.social.web.spider.wechat.service.GetApiService;
import com.github.starry.fw.social.web.spider.wechat.service.impl.GetApiServiceImpl1;

@Component
public class GetApiTasklet implements Tasklet {


    private String directory = "D:/Cache/git/starry-fw-social-web-spider/html";

    private String baseUrl = "https://api.weixin.qq.com/";

    @Autowired
    private GetApiServiceImpl1 impl1;

    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        File directoryFile = new File(directory);

        File[] files = directoryFile.listFiles();

        Map<String,GetApiService> fileMap = getFileMap();
        for(File file : files) {
//            getApi(file);
//
            GetApiService service = fileMap.get(file.getName());

            if(service != null) {
                service.getApi(file);
            }
        }

        return RepeatStatus.FINISHED;
    }

    private Map<String,GetApiService> getFileMap() {

        Map<String,GetApiService> result = new HashMap<String,GetApiService>();


        result.put("41.html", impl1);

        return result;

    }

    private void getApi(File file) throws IOException {
        Document doc = Jsoup.parse(file, "UTF-8");

        Iterator<Element> linkIterator = doc.select(".external.free").iterator();

        while(linkIterator.hasNext()){
            Element link = linkIterator.next();

            String href =link.attr("href");

            if(!StringUtils.startsWith(href, baseUrl) || StringUtils.equals(href, baseUrl)) {
                continue;
            }

            String method = searchApiMethod(doc,link);

            if(StringUtils.isEmpty(method)) continue;

            List<ApiParameter> paramList = searchApiParameter(doc,link);

            System.out.println(method + "" +link.attr("href") + " " + file.getName());
            for(ApiParameter param : paramList) {
                System.out.println("    " + param.toString());
            }
        }
    }

    private String searchApiMethod(Document doc, Element link) {

        String method = getApiMethod(link.parent().html());
        if(StringUtils.isNotEmpty(method)) return method;

        method = getApiMethod(link.parent().previousElementSibling().html());
        if(StringUtils.isNotEmpty(method)) return method;


        return "";
    }

    private String getApiMethod (String html) {
        Pattern p = Pattern.compile("请求方式(:|：) *(?<HttpMethod>GET|POST)");
        Matcher m = p.matcher(html);
        while(m.find()) {
            String g = m.group("HttpMethod");
            return g;
        }

        return "";
    }

    private List<ApiParameter> searchApiParameter(Document doc, Element link) {

        Pattern p = Pattern.compile("参数");

        Element next = link.parent().nextElementSibling();

        while(next!=null) {

            if(p.matcher(next.html()).find() && StringUtils.equalsIgnoreCase("table", next.nextElementSibling().tagName()) ) {
                return getParameter(next.nextElementSibling());
            }

            next = next.nextElementSibling();
        }


        return new ArrayList<ApiParameter>();
    }

    private List<ApiParameter> getParameter(Element table) {

        List<ApiParameter> paramList = new ArrayList<ApiParameter>();

        Iterator<Element> trIterator = table.select("tr").iterator();

        while(trIterator.hasNext()){
            List<String> itemValues = new ArrayList<String>();
            Iterator<Element> tdIterator = trIterator.next().select("td").iterator();
            while(tdIterator.hasNext()){
                itemValues.add(tdIterator.next().html());
            }

            if(CollectionUtils.isEmpty(itemValues) || itemValues.size() != 3) {
                continue;
            }

            paramList.add(new ApiParameter(itemValues.get(0),itemValues.get(1),itemValues.get(2)));
        }

        return paramList;
    }

}